package com.example.hms.service.servicelmpl;

import com.example.hms.dao.LiveDao;
import com.example.hms.dao.LiveRecordsDao;
import com.example.hms.dao.StayDao;
import com.example.hms.entity.Live;
import com.example.hms.entity.LiveRecords;
import com.example.hms.entity.Stay;
import com.example.hms.entity.SystemLog;
import com.example.hms.service.LiveRecordsService;
import com.example.hms.service.StayService;
import com.example.hms.service.SystemLogService;
import com.example.hms.util.Result;
import com.example.hms.util.ResultSubmit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;


@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
public class StayServiceImpl implements StayService {
    @Resource
    StayDao stayDao;

    @Resource
    LiveDao liveDao;

    @Resource
    LiveRecordsService liveRecordsService;

    @Resource
    SystemLogService systemLogService;

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    @Override
    public Result selectAllStay(Stay stay) {
        return ResultSubmit.success(stayDao.selectAllStay(stay));
    }

    @Override
    public Result insertStay(Live live, HttpServletRequest request) {
        live.setDeleteIdentity("未删除");
        live.setLiveTime(format.format(new Date()));
        live.setLiveId(Math.abs((int) System.currentTimeMillis()));
        live.setAbrogateIdentity("未作废");
        live.setCollectionIdentity("未收款");
        live.setDataUpdateTime(format.format(new Date()));

        LiveRecords liveRecords = new LiveRecords();
        liveRecords.setLiveId(live.getLiveId());
        liveRecords.setLiveRecordsId(Math.abs((int) System.currentTimeMillis()));
        liveRecords.setCustomerId(live.getCustomerId());
        liveRecords.setRoomId(live.getRoomId());
        liveRecords.setDataUpdateTime(format.format(new Date()));
        liveRecords.setDeleteIdentity("未删除");

        int i = liveDao.insert(live);

        Live live1 = liveDao.selectAllLive(live).get(0);

        // 系统记录
        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("live");
        systemLog.setUseTableDataIdAfter(String.valueOf(live1.getLiveTableId()));

        systemLog.setUseType("用户入住");
        systemLogService.insert(systemLog);


        if (i == 1 && liveRecordsService.insert(liveRecords) == 1) {
            return ResultSubmit.success(live.getLiveId());
        } else {
            // 手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.fail();
        }
    }

    @Override
    public Result cancelStay(Live live, HttpServletRequest request) {
        live = liveDao.selectAllLive(live).get(0);
        int i = liveDao.update(live);
        live.setCancelTime(format.format(new Date()));
        int j = liveDao.insert(live);
        String liveId = String.valueOf(live.getLiveTableId());
        live.setLiveTableId(null);
        live.setState(null);
        live.setAbrogateIdentity(null);

        Live live1 = liveDao.selectAllLive(live).get(0);

        // 系统记录
        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("live");
        systemLog.setUseTableDataIdAfter(liveId);
        systemLog.setUseTableDataIdBefore(String.valueOf(live1.getLiveTableId()));

        systemLog.setUseType("用户退房");
        systemLogService.insert(systemLog);

        if (i == 1 && j == 1) {
            return ResultSubmit.success("执行成功");
        } else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.fail();
        }

    }

    @Override
    public Result abrogateStay(Live live, HttpServletRequest request) {
        live = liveDao.selectAllLive(live).get(0);
        int i = liveDao.update(live);
        live.setAbrogateIdentity("已作废");
        int j = liveDao.insert(live);

        live.setLiveTableId(null);
        live.setState(null);
        live.setAbrogateIdentity(null);


        Live live1 = liveDao.selectAllLive(live).get(0);

        // 系统记录
        SystemLog systemLog = new SystemLog();
        systemLog.setUserId((Integer) request.getSession().getAttribute("userId"));
        systemLog.setRoleName((String) request.getSession().getAttribute("roleName"));
        systemLog.setCreateTime(format.format(new Date()));
        systemLog.setUseTableName("live");

        systemLog.setUseTableDataIdBefore(String.valueOf(live1.getLiveTableId()));

        systemLog.setUseType("用户作废");
        systemLogService.insert(systemLog);

        if (i == 1 && j == 1) {
            return ResultSubmit.success("执行成功");
        } else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.fail();
        }

    }
}
